package com.cykj.eurekaclient.controller;

import com.cykj.eurekaclient.bean.BackUser;
import com.cykj.eurekaclient.bean.DriverSalaryRecord;
import com.cykj.eurekaclient.bean.DriverSchedule;
import com.cykj.eurekaclient.service.DriverService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DriverController {

    @Autowired
    private DriverService driverService;

    @RequestMapping("/login")
    @ResponseBody
    public BackUser login(@RequestBody Map<String, String> data) {
        BackUser driver = driverService.login(data.get("account"), data.get("password"), data.get("phone"));
        if (driver != null){
            if(Integer.getInteger(String.valueOf(driver.getAccountState())) != 4) {
                return driver;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    @RequestMapping("/salary")
    public Map<String, Object> salary(String limit, String page, String driverId, String date){
        System.out.println(limit+" "+page+" "+driverId+" "+date);
        Map<String, Object> msg = new HashMap<>();
        RowBounds rowBounds = new RowBounds((Integer.valueOf(page)-1)*Integer.valueOf(limit), Integer.valueOf(limit));
        List<DriverSalaryRecord> salaryList = driverService.salaryList(Integer.valueOf(driverId), rowBounds, date);
        int total = driverService.salaryRecord(Integer.valueOf(driverId));
        msg.put("salaryList", salaryList);
        msg.put("total", total);
        return msg;
    }

    @RequestMapping("/record")
    public Map<String, Object> record(String limit, String page, String driverId, String date){
        Map<String, Object> msg = new HashMap<>();
        String startDate = "";
        String endDate = "";
        if(date != ""){
            String[] dates = date.split("/");
            startDate = dates[0];
            endDate = dates[1];
        }
        RowBounds rowBounds = new RowBounds((Integer.valueOf(page)-1)*Integer.valueOf(limit), Integer.valueOf(limit));
        List<DriverSchedule> recordList = driverService.recordList(Integer.valueOf(driverId), rowBounds ,
                startDate, endDate);
        int total = driverService.departureRecord(Integer.valueOf(driverId));
        msg.put("recordList", recordList);
        msg.put("total", total);
        return msg;
    }

    @RequestMapping("/timeTable")
    public Map<String, Object> timeTable(String limit, String page, String driverId, String date){
        Map<String, Object> msg = new HashMap<>();
        String startDate = "";
        String endDate = "";
        if(date != ""){
            String[] dates = date.split("/");
            startDate = dates[0];
            endDate = dates[1];
        }
        RowBounds rowBounds = new RowBounds((Integer.valueOf(page)-1)*Integer.valueOf(limit), Integer.valueOf(limit));
        List<DriverSchedule> timeList = driverService.timeList(Integer.valueOf(driverId), rowBounds ,
                startDate, endDate);
        int total = driverService.timeTable(Integer.valueOf(driverId));
        msg.put("timeList", timeList);
        msg.put("total", total);
        return msg;
    }

    @RequestMapping("/workload")
    public Map<String, Object> workload(String limit, String page, String driverId, String date){
        Map<String, Object> msg = new HashMap<>();
        String startDate = "";
        String endDate = "";
        if(date != ""){
            String[] dates = date.split("/");
            startDate = dates[0];
            endDate = dates[1];
        }
        RowBounds rowBounds = new RowBounds((Integer.valueOf(page)-1)*Integer.valueOf(limit), Integer.valueOf(limit));
        List<DriverSchedule> workload = driverService.workload(Integer.valueOf(driverId), rowBounds ,
                startDate, endDate);
        for (DriverSchedule driverSchedule:workload) {
            if(driverSchedule.getState().equals("23")){
                driverSchedule.setState("已发车");
            }else{
                driverSchedule.setState("未发车");
            }
        }
        int total = driverService.workloadRecord(Integer.valueOf(driverId));
        msg.put("workload", workload);
        msg.put("total", total);
        return msg;
    }

    @RequestMapping("/departure")
    public Map<String, Object> departure(String driverId, String date, String time, String nearTime){
        Map<String, Object> msg = new HashMap<>();
        List<DriverSchedule> checkList = driverService.checkList(Integer.valueOf(driverId), date, time);
        List<DriverSchedule> busList = driverService.busList(Integer.valueOf(driverId), date, time, nearTime);
        msg.put("checkList", checkList);
        msg.put("busList", busList);
        return  msg;
    }

    @RequestMapping("/check")
    public boolean check(String driverId, String id){
        if (driverService.departure(Integer.valueOf(driverId), Integer.valueOf(id))){
            return true;
        }else{
            return false;
        }
    }

    @RequestMapping("/deduction")
    public Map<String, Object> deduction(String limit, String page, String driverId, String date){
        Map<String, Object> msg = new HashMap<>();
        String startDate = "";
        String endDate = "";
        if(date != ""){
            String[] dates = date.split("/");
            startDate = dates[0];
            endDate = dates[1];
        }
        RowBounds rowBounds = new RowBounds((Integer.valueOf(page)-1)*Integer.valueOf(limit), Integer.valueOf(limit));
        List<DriverSalaryRecord> deductionList = driverService.deductionList(Integer.valueOf(driverId), rowBounds ,
                startDate, endDate);
        int total = driverService.deductionRecord(Integer.valueOf(driverId));
        msg.put("deductionList", deductionList);
        msg.put("total", total);
        return msg;
    }

    @RequestMapping("/scheduling")
    public List<DriverSchedule> schedules(String driverId){
        List<DriverSchedule> schedules = driverService.schedules(Integer.valueOf(driverId));
        for(DriverSchedule schedule: schedules){
            schedule.setInfo("排班");
        }
        return schedules;
    }

    @RequestMapping("/leave")
    public boolean leave(String driverId, String date){
        if (driverService.leave(Integer.valueOf(driverId), date)){
            return true;
        }else{
            return false;
        }
    }
}
